package com.sikao.app.ui;

import android.os.Bundle;
import android.support.v4.app.FragmentManager;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.TabHost;
import android.widget.TextView;
import android.widget.Toast;

import com.sikao.app.R;
import com.sikao.app.fragment.Fragment1;
import com.sikao.app.fragment.Fragment2;
import com.sikao.app.fragment.Fragment3;
import com.sikao.app.fragment.Fragment4;
import com.sikao.app.fragment.Fragment5;
import com.sikao.app.net.UpdateApk;
import com.sikao.app.ui.base.BaseActivity;
import com.sikao.app.view.FragmentTabHost;

/**
 * 主activity，首页、商城、招标、拍卖、我的这五个fragment的寄宿者和管理者
 */
public class FirstActivity extends BaseActivity {

	//按返回键时的时间
	private long firstTime;

	private LayoutInflater layoutInflater;
	private FragmentTabHost tabhost;
	private FragmentManager fragmentManager;
	private String tabHostTagArray[] = {"主页", "商城", "招标", "拍卖", "我的"};
	private String currentTab = tabHostTagArray[0];
	private Class fragmentArray[] = {Fragment1.class, Fragment2.class, Fragment3.class, Fragment4.class, Fragment5.class};
	private int mImageViewArray[] = {R.drawable.ic_tab1, R.drawable.ic_tab2, R.drawable.ic_tab3,
			R.drawable.ic_tab4, R.drawable.ic_tab5};

	public FragmentTabHost getTabhost() {
		return tabhost;
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		setContentView(R.layout.activity_first);
		super.onCreate(savedInstanceState);
	}

	@Override
	protected void initData() {
		layoutInflater = getLayoutInflater();
	}

	@Override
	protected void initView() {
		tabhost = (FragmentTabHost) findViewById(android.R.id.tabhost);
		fragmentManager = getSupportFragmentManager();
		tabhost.setup(this, getSupportFragmentManager(), R.id.realtabcontent);

		int count = fragmentArray.length;
		for (int i = 0; i < count; i++) {
			//为每一个Tab按钮设置图标、文字和内容
			TabHost.TabSpec tabSpec = tabhost.newTabSpec(tabHostTagArray[i]).setIndicator(getTabItemView(i));
			//将Tab按钮添加进Tab选项卡中
			tabhost.addTab(tabSpec, fragmentArray[i], null);
			//设置Tab按钮的背景\
			tabhost.getTabWidget().setBackgroundColor(getResources().getColor(R.color.window_bg));

		}
		tabhost.setOnTabChangedListener(tabChangeListener);
	}

	@Override
	protected void initListener() {
		UpdateApk updateApk = new UpdateApk(this, false);
		updateApk.checkUpdateInfo(false);
	}

	@Override
	protected void init() {
		tabhost.getTabWidget().setDividerDrawable(null);
	}

	private TabHost.OnTabChangeListener tabChangeListener = new TabHost.OnTabChangeListener() {
		@Override
		public void onTabChanged(String tabId) {
			currentTab = tabId;
			if (tabId.equals("我的")) {
				String a = tabhost.getCurrentTabTag();
				Fragment5 fragment5 = (Fragment5) fragmentManager.findFragmentByTag(tabhost.getCurrentTabTag());
				if (fragment5 != null){
					fragment5.onResume();
				}
			}
		}
	};

	/**
	 * 给Tab按钮设置图标和文字
	 */
	private View getTabItemView(int index) {
		View view = layoutInflater.inflate(R.layout.tabhost_item, null);

		ImageView imageView = (ImageView) view.findViewById(R.id.imageview);
		imageView.setImageResource(mImageViewArray[index]);
		TextView textView = (TextView) view.findViewById(R.id.textview);
		textView.setText(tabHostTagArray[index]);
		return view;
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK) {
			if ((System.currentTimeMillis() - firstTime) > 2000) {
				firstTime = System.currentTimeMillis();
				Toast.makeText(this, "再按一次退出程序", Toast.LENGTH_SHORT).show();
				return false;
			} else {
				appManager.AppExit(this);
			}
		}
		return super.onKeyDown(keyCode, event);
	}

	private void getEquipmentInfo() {
		DisplayMetrics metric = new DisplayMetrics();
		getWindowManager().getDefaultDisplay().getMetrics(metric);
		// 屏幕宽度（像素）
		int width = metric.widthPixels;
		// 屏幕高度（像素）
		int height = metric.heightPixels;
		// 屏幕密度（0.75 / 1.0 / 1.5）
		float density = metric.density;
		float scaledDensity = metric.scaledDensity;
		float xdpi = metric.xdpi;
		// 屏幕密度DPI（120 / 160 / 240）
		int densityDpi = metric.densityDpi;
		Log.d("first", "width>>" + width);
		Log.d("first", "height>>" + height);
		Log.d("first", "density>>" + density);
		Log.d("first", "densityDpi>>" + densityDpi);
		Log.d("first", "scaledDensity>>" + scaledDensity);
		Log.d("first", "xdpi>>" + xdpi);
		String CacheDir = this.getCacheDir().getAbsolutePath();
		Log.d("first", "CacheDir>>" + CacheDir);
		String FilesDir = this.getFilesDir().getAbsolutePath();
		Log.d("first", "FilesDir>>" + FilesDir);
		String ExternalCacheDir = this.getExternalCacheDir().getAbsolutePath();
		Log.d("first", "ExternalCacheDir>>" + ExternalCacheDir);
	}

}